package com.lujianfei.netease_android_canvas.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.lujianfei.netease_android_canvas.utils.BezierUtils

/**
 * 贝塞尔曲线绘制组件
 */
class QuadBezierView : View {

    // 控制点
    private var eventX = 0f
    private var eventY = 0f
    // 起点
    private var startX = 0f
    private var startY = 0f
    // 终点
    private var endX = 0f
    private var endY = 0f
    // 控制点比例
    private var x1 = 0f
    private var y1 = 0f
    // 坐标圆点1，纵轴方向
    private var c1_x = 0f
    private var c1_y = 0f
    // 坐标圆点2，曲线路径
    private var c2_x = 0f
    private var c2_y = 0f
    // 坐标圆点3，横轴方向
    private var c3_x = 0f
    private var c3_y = 0f

    // 绘制对象
    private var paint: Paint? = null
    private val padding = 50

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    private fun init() {
        paint = Paint()
        paint?.apply {
            isAntiAlias = true
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

        startX = padding.toFloat()
        startY = h.toFloat() - padding.toFloat()
        endX = w.toFloat() - padding.toFloat()
        endY = padding.toFloat()

        eventX = endX * x1
        eventY = y1 * endY
    }

    fun setControlPoint(x: Float, y: Float) {
        x1 = x
        y1 = y
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        paint?.let { paintNonNull ->
            paintNonNull.color = Color.YELLOW
            //画3个点
            canvas!!.drawCircle(c1_x, c1_y, 20f, paint!!)
            canvas.drawCircle(c2_x, c2_y, 20f, paint!!)
            canvas.drawCircle(c3_x, c3_y, 20f, paint!!)

            //绘制连线
            paintNonNull.color = Color.GREEN
            paintNonNull.strokeWidth = 3f
            canvas.apply {
                drawLine(startX, startY, startX, endY, paintNonNull)
                drawLine(startX, startY, endX, startY, paintNonNull)

                //3点连线
                drawLine(c1_x, c1_y, c2_x, c2_y, paintNonNull)
                drawLine(c2_x, c2_y, c3_x, c3_y, paintNonNull)
            }


            //画二阶贝塞尔曲线
            paintNonNull.color = Color.RED
            paintNonNull.style = Paint.Style.STROKE
            val path = Path()
            path.moveTo(startX, startY)
            path.quadTo(eventX, eventY, endX, endY)
            canvas.drawPath(path, paintNonNull)
        }
    }

    fun setCurrentPos(pos: Float) {
        // circle 3 (x)
        c3_x = pos * (endX - startX) + padding
        c3_y = startY

        // circle 2 (Quad Bezier)
        val t = BezierUtils.getT(input = c3_x, p0 = startX, p1 = eventX, p2 = endX)
        c2_x = BezierUtils.quadBezier(t = t, p0 = startX, p1 = eventX, p2 = endX)
        c2_y = BezierUtils.quadBezier(t = t, p0 = startY, p1 = eventY, p2 = endY)

        // circle 1
        c1_x = startX
        c1_y = c2_y

        invalidate()
    }
}